package com.xb.xbpm.controller;

import com.xb.xbpm.service.ISiteService;
import com.xb.xbpm.utils.EsUtils;
import com.xb.xbpm.utils.HttpClientUtil;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作弊分析——推手_小说|电影
 * 索引——cc_yyyy-MM-dd[小说] | ff_yyyy-MM-dd[电影]
 * @Author gxd
 * @Date： 2018/10/17
 */
@Controller
@RequestMapping(value = "cheat")
public class CheatController {

    @Autowired
    EsUtils esUtils;
    @Autowired
    ISiteService siteService;


    /**
     * 小说——获取作弊列表
     * @param request
     * @return
     */
    @RequestMapping("/getList")
    public @ResponseBody
    Map<String, Object> getList(HttpServletRequest request){
        ModelMap map=new ModelMap();
        List<Map<String, Object>> cheatList=new ArrayList<Map<String,Object>>();

        List<String> datelist=null;

        String startDate="";
        String endDate="";
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");
        //小说type2 活动type3
        String type=request.getParameter("type");

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyy-MM-dd");
        }

        try {
            //拼接索引
            String index="";
            if(Tools.isEmpty(endDate) || startDate.equals(endDate)){
                //一天的
                index="cc_"+startDate;
            }else{
                //时间段内的所有索引值
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                Date start=null;
                Date end=null;

                start = format1.parse(startDate);
                end= format1.parse(endDate);
                datelist= getBetweenDates(start,end);
                datelist.add(endDate);

                for (String s: datelist ) {
                    index+="cc_"+s+",";
                }

            }

            //查找作弊列表
            cheatList= esUtils.getCheatListByType(index,type);
            if(cheatList.size()>50){
                cheatList=cheatList.subList(0,50);
            }

        }catch (Exception e){
            e.getStackTrace();
        }

        cheatList.parallelStream().forEach(x->{
            String stats=getStatsBySid(x.get("sid").toString());
            x.put("stats",stats);
        });

        map.put("cheatList",cheatList);

        return map;
    }


    //=========================推手电影作弊分析相关==========================
    /**
     * 电影——获取作弊列表
     * @param request
     * @return
     */
    @RequestMapping("/getFilmList")
    public @ResponseBody
    Map<String, Object> getFilmList(HttpServletRequest request){
        ModelMap map=new ModelMap();


        List<Map<String, Object>> cheatList=new ArrayList<Map<String,Object>>();

        List<String> datelist=null;


        String startDate="";
        String endDate="";
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");


        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyyMMdd");
        }

        try {
            //拼接索引
            String index="";
            if(Tools.isEmpty(endDate) || startDate.equals(endDate)){
                //一天的
                index="ff_"+startDate.replaceAll("-","");
            }else{
                //时间段内的所有索引值
                DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
                Date start=null;
                Date end=null;

                start = format1.parse(startDate);
                end= format1.parse(endDate);
                datelist= getBetweenDates2(start,end);
                datelist.add(endDate);

                for (String s: datelist ) {
                    index+="ff_"+s+",";
                }

            }
            //查找作弊列表
            cheatList= esUtils.getCheatList(index);
            if(cheatList.size()>100){
                cheatList=cheatList.subList(0,100);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        /*for (Map<String,Object> map1:cheatList){
            String stats=getStatsBySid(map1.get("sid").toString());
            map1.put("stats",stats);
        }*/
        cheatList.parallelStream().forEach(x->{
            String stats=getStatsBySid(x.get("sid").toString());
            x.put("stats",stats);
        });
        map.put("cheatList",cheatList);

        return map;
    }



    //=========================CPA查询作弊相关================================

    /**
     * 获取cpa问题列表
     * @param request
     * @return
     */
    @RequestMapping("/getCpaList")
    public @ResponseBody
    Map<String, Object> getCpaList(HttpServletRequest request){
        ModelMap map=new ModelMap();


        List<Map<String, Object>> cheatList=new ArrayList<Map<String,Object>>();

        List<String> datelist=null;


        String startDate="";
        String endDate="";
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");


        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyyMMdd");
        }

        try {
            //拼接索引
            String index="";
            if(Tools.isEmpty(endDate) || startDate.equals(endDate)){
                //一天的
                index="cpa_"+startDate.replaceAll("-","");
            }else{
                //时间段内的所有索引值
                DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
                Date start=null;
                Date end=null;

                start = format1.parse(startDate);
                end= format1.parse(endDate);
                datelist= getBetweenDates2(start,end);
                datelist.add(endDate);

                for (String s: datelist ) {
                    index+="cpa_"+s+",";
                }

            }
            //查找作弊列表
            cheatList= esUtils.getCpaCheatList(index);
            if(cheatList.size()>100){
                cheatList=cheatList.subList(0,100);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        cheatList.parallelStream().forEach(x->{
            String stats=getStatsBySid(x.get("sid").toString());
            x.put("stats",stats);
        });
        map.put("cheatList",cheatList);

        return map;
    }





    /*@RequestMapping("/getStatsBySid")
    public @ResponseBody
    Map<String, Object> getStatsBySid(HttpServletRequest request){
        ModelMap map=new ModelMap();
        String sid=request.getParameter("sid");
        String result="";
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("sid", sid);
            result = HttpClientUtil.doGet("http://t.xinba.com/tung_splinter_status", param);
            if("0".equals(result)){
                result="锁定";
            }else if("1".equals(result)){
                result="正常";
            }else if("3".equals(result)){
                result="删除";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        map.put("result",result);
        return map;
    }
*/
    private  String getStatsBySid(String sid) {
        String result="1";
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("sid", sid);
            result = HttpClientUtil.doGet("http://t.xinba.com/tung_splinter_status", param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取两个日期之间的日期
     * @param start 开始日期
     * @param end 结束日期
     * @return 日期集合
     */
    private static List<String> getBetweenDates(Date start, Date end) {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(format1.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    private static List<String> getBetweenDates2(Date start, Date end) {
        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(format1.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }
}
